$(function() {
	$('.header-menu,.medium-menu-tooltip').hover( function() {
		showtooltip($(this).attr('name'),$(this).offset().left,$(this).offset().top, $(this).width(), $(this).height());
	}, function() {
		$('#ctooltip').remove();
	});		
	/*
	$('.popup-text').hover(function() {
		alert();
		showtooltip($(this).attr('title'),$(this).offset().left,$(this).offset().top, $(this).width(), $(this).height(), "smaller");		
	}, function() {
		$('#ctooltip1').remove();		
	});
	*/	
	
	$(window).scroll(function () {
				//fix_pb();
		});
	
	add_eventHandlers_leftCol();
	
});

function add_eventHandlers_leftCol()
{
	$('.groupLink').parent().hover(function() {
		$(this).addClass('hover-leftcol');
	},function () {
		$(this).removeClass('hover-leftcol');
	});

	$('.groupLink').click(function () {
		var name = $(this).attr('id');
		var arr = name.split("_");
		var gpid = arr[1]; // collecting the gpid
		$('.groupLink').css('font-weight','normal');
		$('.groupLink').parent().removeClass('highlight-leftcol');
		$('.group_friendList').html('');
		$('.group_friendList_handle').removeClass('a');
		$('.groupSettingsLink').removeClass('groupSettingsLinkSel');

		$(this).css('font-weight','bold');
		$(this).parent().addClass('highlight-leftcol');
		$(this).parent().find('.group_friendList_handle').addClass('a');
		$(this).parent().find('.unread').hide();
		
		
		$('#left-pane').html('<img src="images/22.gif" style="margin:20px"/>');		
		$.ajax({
			type : "POST",
			url : "php/modules/group.php",
			async : true,
			data : "gpid=" + gpid,
			success : function(data) {
				$('#left-pane').html(data);
				//$('#right-pane-content').lionbars();
				window.history.pushState({"html":data,"pageTitle":"Gausipp | group"},"", "index.php?cat=group&gpid="+gpid+"&action=gausipp");
			}
		});
		$.ajax({
			type : "POST",
			url : "php/sidebar/group_sidebar.php",
			async : true,
			data : "gpid=" + gpid,
			success : function(data) {	
				$('#group_'+gpid+'_friendsList').html(data);			
				//$('#right-pane-content').lionbars();				
			}
		});
	});
	
	$('.groupSettingsLink').click(function () {
		var name = $(this).attr('id');
		var arr = name.split("_");
		var gpid = arr[1]; // collecting the gpid
		$('.groupLink').css('font-weight','normal');
		$('.group_friendList').html('');
		$('.groupSettingsLink').removeClass('groupSettingsLinkSel');

		//alert("group_"+gpid+"_sidebarLink");
		$("span#group_"+gpid+"_sidebarLink").css('font-weight','bold');
		$(this).addClass('groupSettingsLinkSel');
		
		
		$('#left-pane').html('<img src="images/22.gif" style="margin:20px"/>');		
		$.ajax({
			type : "POST",
			url : "php/modules/group_info.php",
			async : true,
			data : "gpid=" + gpid,
			success : function(data) {
				$('#left-pane').html(data);
				//$('#right-pane-content').lionbars();
				window.history.pushState({"html":data,"pageTitle":"Gausipp | Settings"},"", "index.php?cat=group&gpid="+gpid+"&action=info");
			}
		});
	});
	
	$('#new_circle_button').click(function() {
		$('.groupLink').css('font-weight','normal');
		$('.group_friendList').html('');
		$('.groupLink').parent().removeClass('highlight-leftcol');
		$('.group_friendList_handle').removeClass('a');

		$('#left-pane').html('<img src="images/22.gif" style="margin:20px"/>');		
			$.ajax({
				type : "POST",
				url : "php/modules/new_group.php",
				async : true,
				data : "ajax=true",
				success : function(data) {				
					$('#left-pane').html(data);
					//$('#right-pane-content').lionbars();
					window.history.pushState({"html":data,"pageTitle":"Gausipp | New Circle"},"", "index.php?cat=group&action=create");
				}
			});
	});
}

function fix_pb()
{
	var thresh = ($('#stream_wrapper').offset().top+$('#stream_wrapper').height() - 150) -( $(window).height() - 150);
		if( $(window).scrollTop()  < thresh)
		    $('#pb_wrapper').addClass("bottom-float");
		else 
			$('#pb_wrapper').removeClass("bottom-float");
}

function showtooltip(contents, x, y, width, height, mode) {	
        if(mode == "smaller")
        {
        	$('<div id="ctooltip1">' + contents + '</div>').css( {
		            position: 'absolute',
		            display: 'none',
		            top: y + height,
		            left: x,
		            border: '1px solid #aaa',
		             'box-shadow': ' 2px 2px 2px #777',
		            padding: '5px',
		            'background-color': '#00',
		            opacity: 0.90,
		            color: '#fff', 
		            'z-index': 101,
		        }).appendTo("body").fadeIn(200);
        }
        else
        {
			 $('<div id="ctooltip">' + contents + '</div>').css( {
		            position: 'absolute',
		            display: 'none',
		            top: y + height - 10,
		            left: x +5,
		            border: '1px solid #aaa',
		             'border-radius': '5px',
		             'box-shadow': ' 2px 2px 5px #777',
		            padding: '5px',
		            'background-color': '#fff',
		            opacity: 0.90,
		            color: '#444', 
		            'z-index': 101,
		        }).appendTo("body").fadeIn(200);
       }
}
	
	
function save_settings(element, type)
{
	
	
	if(type == "sub")
	{
		var selector = $(element).parent().find('select');
		var id = selector.attr('id');
		var value = selector.val();
	}
	
	//alert("id="+id+"&value="+value);
	$(element).attr('disabled',true).val('saving');  
	 
	$.ajax({
		type: "POST",
		url: "php/ajax/save_settings.php",
		data: "id="+id+"&value="+value,
		success: function(msg) {
			if(msg!="") alert(msg);
			else	
				{
					show_msg('<span class="settings_msg">saved</span>',"alert");
					$(element).attr('disabled',false).val('save');					
				}
		}		
	});
	
}

function tourme(where, name){
		
		if(where == "home"){
		
		if(name == "hide")
			 { $('#float_box_wrapper').fadeOut(); return; }
	 
			var next = "";  // the next div to illustrate	
			var disp_text = ""; // the text to display in the box
			if(name == "menu1")
				{
				next = "menu2";
				disp_text = "<p>This is where you see the feed from your circles and subscriptions.</p>";
				dir = "up";
				}
			else if(name == "menu2")
				{
				dir = "left";
				next = "new_circle_button";
				disp_text = "<p>And this is where you see the feed from all the public and protected circles on Gausipp!</p>";
				}
			else if(name == "new_circle_button")
				{
				dir = "right";
				next = 0;
				disp_text = "<p>This is where you create new circles!</p>";
				}	
	
			//getting the postion of the div to illustrate
			var pos = $('#'+name+'').offset();
			var height = $('#'+name+'').height();
			var width = $('#'+name+'').width();
			pos.left = Math.floor(pos.left);
			pos.top = Math.floor(pos.top);
			
			//alert(''+pos.left +' '+ pos.top+'');
	
			if(dir == "up")
			{
				var pleft = pos.left+10;
				var ptop = pos.top+height;
				$('#float_box_wrapper').html('<div id="arr"><div id="tri" class="tri_up"></div></div><div id="main2"></div>');
			}
			else if(dir == "down")
			{
				var pleft = pos.left+10;
				var ptop = pos.top-120;
				$('#float_box_wrapper').html('<div id="main2"></div><div id="arr"><div id="tri" class="tri_down"></div></div>');
			}
			else if(dir == "left")
			{
				var pleft = pos.left+width;
				var ptop = pos.top-10;
				$('#float_box_wrapper').html('<div id="arr" style="float:left"><div id="tri" class="tri_left"></div></div><div id="main2" style="float:left"></div>');
			}
			else if(dir == "right")
			{		
				var pleft = pos.left-width-10;
				var ptop = pos.top-10;
				$('#float_box_wrapper').html('<div id="main2" style="float:left"></div><div id="arr" style="float:left"><div id="tri" class="tri_right"></div></div>');
			}	
	
	
			
	
			//showing the div
			$('#float_box_wrapper').hide();
			if(next!=0)
				$('#main2').html(''+disp_text+'<button onclick=\'tourme("'+where+'","'+next+'")\'>next</button> <button onclick=\'tourme("'+where+'","hide")\'>later</button>');
			else
				$('#main2').html(''+disp_text+'<button onclick=\'tourme("'+where+'","hide")\'>close</button>');
			
			$('#float_box_wrapper').css('left',pleft).css('top',ptop).fadeIn();
	} // for where condition equal to home
}
